﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lists;
using Excel = Microsoft.Office.Interop.Excel;
using System.Collections;
using System.Data.SqlClient;
using GeneralLibs;

namespace ListForms
{
    public partial class FrmEditHangHoa : ListForms.FrmBaseEditForm
    {
        public FrmEditHangHoa(Lists.VatTuHangHoa VatTuHangHoa)
        {
            try
            {
                InitializeComponent();
                _DoiTuong = VatTuHangHoa;

                BasicForms.FrmBase.FormatTextEdit(txtGiaMua, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtGiaBan, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtGiaBia, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBan, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauMua, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBanLe, BasicForms.TextFormatType.Money);
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        public FrmEditHangHoa(string ten)
        {
            try
            {
                InitializeComponent();
                _DoiTuong = null;
                txtTen.EditValue = ten;

                BasicForms.FrmBase.FormatTextEdit(txtGiaMua, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtGiaBan, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtGiaBia, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBanLe, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBan, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauMua, BasicForms.TextFormatType.Money);

                cmbNhomVatTuHangHoa.Focus();
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }
        public FrmEditHangHoa(string ma, string ten)
        {
            try
            {
                InitializeComponent();
                _DoiTuong = null;
                txtMa.EditValue = ma;
                txtTen.EditValue = ten;

                BasicForms.FrmBase.FormatTextEdit(txtGiaMua, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtGiaBan, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtGiaBia, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBanLe, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBan, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauMua, BasicForms.TextFormatType.Money);

                cmbNhomVatTuHangHoa.Focus();
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }
        public FrmEditHangHoa(string ma, string ten, Guid doiTuongNghiepVuID)
        {
            try
            {
                InitializeComponent();

                cmbNCC.DataSource = DanhMucDoiTuongNghiepVu.GetDanhMucNhaCungCap().DataView;
                cmbNCC.DisplayColumn = "Ten";
                cmbNCC.ValueColumn = "ID";
                cmbNCC.AddVisibleColumn("Ma", "Mã");
                cmbNCC.AddVisibleColumn("Ten", "Tên");

                _DoiTuong = null;
                txtMa.EditValue = ma;
                txtTen.EditValue = ten;
                cmbNCC.EditValue = doiTuongNghiepVuID;

                BasicForms.FrmBase.FormatTextEdit(txtGiaMua, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtGiaBan, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtGiaBia, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBanLe, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauBan, BasicForms.TextFormatType.Money);
                BasicForms.FrmBase.FormatTextEdit(txtChietKhauMua, BasicForms.TextFormatType.Money);

                cmbNhomVatTuHangHoa.Focus();
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        protected override string GetMa()
        {
            return this.txtMa.Text.Trim();
        }
        protected override void GenNewBarCode(bool isNull)
        {
            try
            {
                if (isNull)
                {
                    txtMa.EditValue = DanhMucVatTuHangHoa.GenBarCode();
                }
                else
                {
                    txtMa.EditValue = null;
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        protected override void FillData()
        {
            cmbNhomVatTuHangHoa.DataSource = DanhMucNhomVatTuHangHoa.Filter("LienQuanDenKho = 1").DataView;
            cmbNhomVatTuHangHoa.DisplayColumn = "Ten";
            cmbNhomVatTuHangHoa.ValueColumn = "ID";
            cmbNhomVatTuHangHoa.AddVisibleColumn("Ma", "Mã");
            cmbNhomVatTuHangHoa.AddVisibleColumn("Ten", "Tên");

            if (_DoiTuong == null)
            {
                try
                {
                    cmbNhomVatTuHangHoa.Focus();
                    this.Text = "Thêm mới vật tư hàng hóa - dịch vụ";
                    _DoiTuong = DanhMucVatTuHangHoa.Instance.NewDoiTuong();
                    txtGiaMua.EditValue = (decimal)0;
                    txtGiaBan.EditValue = (decimal)0;
                    txtGiaBia.EditValue = (decimal)0;
                    txtChietKhauBanLe.EditValue = (int)0;
                    txtChietKhauBan.EditValue = (int)0;
                    txtChietKhauMua.EditValue = (int)0;

                    cmbTacGia.DataSource = DanhMucVatTuHangHoa.GetAllTacGia();
                    cmbTacGia.DisplayMember = "TacGia";
                    cmbTacGia.Text = "";

                    cmbNhaXuatBan.DataSource = DanhMucVatTuHangHoa.GetAllNhaXuatBan();
                    cmbNhaXuatBan.DisplayMember = "NhaXuatBan";
                    cmbNhaXuatBan.Text = "";

                    cmbNCC.DataSource = DanhMucDoiTuongNghiepVu.GetDanhMucNhaCungCap().DataView;
                    cmbNCC.DisplayColumn = "Ten";
                    cmbNCC.ValueColumn = "ID";
                    cmbNCC.AddVisibleColumn("Ma", "Mã");
                    cmbNCC.AddVisibleColumn("Ten", "Tên");

                    cmbDonViTinh.DataSource = DanhMucVatTuHangHoa.GetAllDonViTinh();
                    cmbDonViTinh.DisplayMember = "DonViTinh";
                    cmbDonViTinh.Text = "";

                    _EditState = EditState.CreatingNew;
                }
                catch (SqlException sqlEx)
                {
                    throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
                }
                catch (Exception ex)
                {
                    if (ex is CustomException) throw ex;
                    else
                    {
                        throw new Exception(ex.Message);
                    }
                }
            }
            else
            {
                try
                {
                    cmbNhomVatTuHangHoa.Focus();

                    cmbNCC.DataSource = DanhMucDoiTuongNghiepVu.GetDanhMucNhaCungCap().DataView;
                    cmbNCC.DisplayColumn = "Ten";
                    cmbNCC.ValueColumn = "ID";
                    cmbNCC.AddVisibleColumn("Ma", "Mã");
                    cmbNCC.AddVisibleColumn("Ten", "Tên");

                    #region Doc du lieu

                    this.Text = "Sửa thông tin vật tư hàng hóa - dịch vụ";
                    try
                    {
                        txtTen.Text = (_DoiTuong as Lists.VatTuHangHoa).Ten;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtTenNganGon.Text = (_DoiTuong as Lists.VatTuHangHoa).TenNganGon;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        cmbNhaXuatBan.Text = (_DoiTuong as Lists.VatTuHangHoa).NhaXuatBan;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        if ((_DoiTuong as Lists.VatTuHangHoa).DoiTuongNghiepVu != null)
                            cmbNCC.EditValue = (_DoiTuong as Lists.VatTuHangHoa).DoiTuongNghiepVu.ID;
                        //else
                        //    cmbNCC.Text = (_DoiTuong as Lists.VatTuHangHoa).NguonCungCap;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        cmbTacGia.Text = (_DoiTuong as Lists.VatTuHangHoa).TacGia;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtChietKhauBanLe.EditValue = (_DoiTuong as Lists.VatTuHangHoa).ChietKhauBanLe;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtChietKhauBan.EditValue = (_DoiTuong as Lists.VatTuHangHoa).ChietKhauBan;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtChietKhauMua.EditValue = (_DoiTuong as Lists.VatTuHangHoa).ChietKhauMua;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        rdoSuDung.Checked = (_DoiTuong as Lists.VatTuHangHoa).TrangThai;
                        rdoKhongSuDung.Checked = !(rdoSuDung.Checked);
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        cmbNhomVatTuHangHoa.EditValue = (_DoiTuong as Lists.VatTuHangHoa).NhomVatTuHangHoa.ID;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtGiaBan.EditValue = (_DoiTuong as Lists.VatTuHangHoa).GiaBanHienTai;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtGiaMua.EditValue = (_DoiTuong as Lists.VatTuHangHoa).GiaMuaHienTai;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtMa.Text = (_DoiTuong as Lists.VatTuHangHoa).Ma;
                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtGiaBia.EditValue = (_DoiTuong as Lists.VatTuHangHoa).GiaBia;

                    }
                    catch (Exception ex)
                    { }
                    try
                    {
                        txtGhiChu.EditValue = (_DoiTuong as Lists.VatTuHangHoa).GhiChu;
                    }
                    catch
                    { }
                    try
                    {
                        txtKichThuoc.EditValue = (_DoiTuong as Lists.VatTuHangHoa).KichThuoc;
                    }
                    catch
                    { }
                    try
                    {
                        txtSoTrang.EditValue = (_DoiTuong as Lists.VatTuHangHoa).SoTrang;
                    }
                    catch
                    { }
                    #endregion

                    cmbTacGia.DataSource = DanhMucVatTuHangHoa.GetAllTacGia();
                    cmbTacGia.DisplayMember = "TacGia";
                    cmbTacGia.Text = (_DoiTuong as Lists.VatTuHangHoa).TacGia;

                    cmbNhaXuatBan.DataSource = DanhMucVatTuHangHoa.GetAllNhaXuatBan();
                    cmbNhaXuatBan.DisplayMember = "NhaXuatBan";
                    cmbNhaXuatBan.Text = (_DoiTuong as Lists.VatTuHangHoa).NhaXuatBan;

                    cmbDonViTinh.DataSource = DanhMucVatTuHangHoa.GetAllDonViTinh();
                    cmbDonViTinh.DisplayMember = "DonViTinh";
                    cmbDonViTinh.Text = (_DoiTuong as Lists.VatTuHangHoa).DonViTinh;

                    _EditState = EditState.Modifying;

                    txtMa.Properties.ReadOnly = Lists.VatTuHangHoa.DaDuocGiaoDich(_DoiTuong.ID) &&
                        (Config.DuocPhepSuaMaHangKhiDaGiaoDich == DuocPhepSuaMaHangKhiDaGiaoDichHayKhong.KhongDuocPhep);
                }
                catch (Exception ex)
                {
                }
            }
        }
        protected override bool CheckFullInfo()
        {
            if (txtMa.Text == "") throw new CustomException("Chưa điền mã vật tư hàng hóa - dịch vụ", ExceptionType.LackOfInputInfo);
            if (txtTen.Text == "") throw new CustomException("Chưa điền tên vật tư hàng hóa - dịch vụ", ExceptionType.LackOfInputInfo);
            if (cmbNhomVatTuHangHoa.EditValue == null) throw new CustomException("Chưa chọn nhóm vật tư hàng hóa - dịch vụ", ExceptionType.LackOfInputInfo);
            if (txtGiaBan.EditValue == null && txtGiaBan.Enabled) throw new CustomException("Chưa nhập giá bán mặc định", ExceptionType.LackOfInputInfo);
            if (txtGiaMua.EditValue == null && txtGiaMua.Enabled) throw new CustomException("Chưa nhập giá mua mặc định", ExceptionType.LackOfInputInfo);
            if (txtGiaBia.EditValue == null && txtGiaBia.Enabled) throw new CustomException("Chưa nhập Giá bán lẻ mặc định", ExceptionType.LackOfInputInfo);
            if (rdoSuDung.Checked == false && rdoKhongSuDung.Checked == false) throw new CustomException("Chưa chọn hàng sử dụng hay không sử dụng", ExceptionType.LackOfInputInfo);
            if (_EditState == EditState.Modifying && Lists.VatTuHangHoa.DaDuocGiaoDich(_DoiTuong.ID)
                && txtMa.Text != (_DoiTuong as Lists.VatTuHangHoa).Ma && Config.DuocPhepSuaMaHangKhiDaGiaoDich != DuocPhepSuaMaHangKhiDaGiaoDichHayKhong.DuocPhep)
            {
                throw new CustomException("Không được sửa mã của hàng đã được giao dịch");
            }
            return true;
        }

        protected override void SetDoiTuongProperties()
        {
            try
            {
                string maHang = txtMa.Text.Trim();
                string tenHang = txtTen.Text.Trim();

                if (_EditState == EditState.CreatingNew)
                {
                    Lists.VatTuHangHoa vthh = DanhMucVatTuHangHoa.Instance.GetByProperty("Ten", tenHang);
                    if (vthh != null)
                    {
                        //MessageBox.Show("Tên hàng này đã tồn tại trong danh mục với mã: " + vthh.Ma);
                        throw new CustomException("Tên hàng này đã tồn tại trong danh mục với mã: " + vthh.Ma);
                    }
                    else
                    {
                        vthh = DanhMucVatTuHangHoa.Instance.GetByProperty("Ma", maHang);
                        if (vthh != null)
                        {
                            //MessageBox.Show("Mã hàng này đã tồn tại trong danh mục với tên: " + vthh.Ten);
                            throw new CustomException("Mã hàng này đã tồn tại trong danh mục với tên: " + vthh.Ten);
                        }
                    }
                }

                #region Set value
                //default
                (_DoiTuong as Lists.VatTuHangHoa).ChietKhauBan = 0;
                (_DoiTuong as Lists.VatTuHangHoa).ChietKhauMua= 0;
                (_DoiTuong as Lists.VatTuHangHoa).ChietKhauBanLe = 0;

                //buoc phai co
                (_DoiTuong as Lists.VatTuHangHoa).Ma = txtMa.Text.Trim();
                (_DoiTuong as Lists.VatTuHangHoa).Ten = txtTen.Text.Trim();
                (_DoiTuong as Lists.VatTuHangHoa).TrangThai = rdoSuDung.Checked;
                if ((_DoiTuong as Lists.VatTuHangHoa).GiaBia != (decimal)txtGiaBia.EditValue)
                {
                    NhatKySuDung.Insert2DB("Sửa", string.Format("giá bán lẻ hàng {0} từ {1} -> {2} đ", 
                        txtMa.Text, (_DoiTuong as Lists.VatTuHangHoa).GiaBia, txtGiaBia.EditValue));
                    (_DoiTuong as Lists.VatTuHangHoa).GiaBia = (decimal)txtGiaBia.EditValue;
                }
                (_DoiTuong as Lists.VatTuHangHoa).NhomVatTuHangHoa = DanhMucNhomVatTuHangHoa.Instance.GetByID((Guid)cmbNhomVatTuHangHoa.EditValue);
                //khong dien cung duoc
                if (txtTenNganGon.Enabled) (_DoiTuong as Lists.VatTuHangHoa).TenNganGon = txtTenNganGon.Text.Trim();
                if (cmbTacGia.Enabled) (_DoiTuong as Lists.VatTuHangHoa).TacGia = cmbTacGia.Text.Trim();
                (_DoiTuong as Lists.VatTuHangHoa).NguonCungCap = cmbNCC.Text.Trim();
                if (cmbNCC.EditValue != null && cmbNCC.EditValue is Guid && cmbNCC.Enabled)
                {
                    (_DoiTuong as Lists.VatTuHangHoa).DoiTuongNghiepVu = DanhMucDoiTuongNghiepVu.Instance.GetByID((Guid)cmbNCC.EditValue);
                }
                if (cmbNhaXuatBan.Enabled) (_DoiTuong as Lists.VatTuHangHoa).NhaXuatBan = cmbNhaXuatBan.Text.Trim();
                if (txtChietKhauBanLe.Enabled) (_DoiTuong as Lists.VatTuHangHoa).ChietKhauBanLe = Convert.ToDecimal(txtChietKhauBanLe.EditValue);
                if (txtChietKhauBan.Enabled) (_DoiTuong as Lists.VatTuHangHoa).ChietKhauBan = Convert.ToDecimal(txtChietKhauBan.EditValue);
                if (txtChietKhauMua.Enabled) (_DoiTuong as Lists.VatTuHangHoa).ChietKhauMua = Convert.ToDecimal(txtChietKhauMua.EditValue);
                if (txtGiaBan.Enabled) (_DoiTuong as Lists.VatTuHangHoa).GiaBanHienTai = Convert.ToDecimal(txtGiaBan.EditValue);
                else (_DoiTuong as Lists.VatTuHangHoa).GiaBanHienTai = 0;
                if (txtGiaMua.Enabled) (_DoiTuong as Lists.VatTuHangHoa).GiaMuaHienTai = Convert.ToDecimal(txtGiaMua.EditValue);
                else (_DoiTuong as Lists.VatTuHangHoa).GiaMuaHienTai = 0;
                if (cmbDonViTinh.Enabled) (_DoiTuong as Lists.VatTuHangHoa).DonViTinh = cmbDonViTinh.Text.Trim();
                if (txtGhiChu.Enabled) (_DoiTuong as Lists.VatTuHangHoa).GhiChu = txtGhiChu.Text.Trim();
                if (txtKichThuoc.Enabled) (_DoiTuong as Lists.VatTuHangHoa).KichThuoc = txtKichThuoc.Text.Trim();
                if (txtSoTrang.Enabled) (_DoiTuong as Lists.VatTuHangHoa).SoTrang = txtSoTrang.Text.Trim();
                #endregion
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void rdoSuDung_Click(object sender, EventArgs e)
        {
            rdoKhongSuDung.Checked = false; ;
        }

        private void rdoKhongSuDung_CheckedChanged(object sender, EventArgs e)
        {
            rdoSuDung.Checked = false;
        }

        private void txtGiaBan_EditValueChanged(object sender, EventArgs e)
        {
            txtGiaBia.EditValue = txtGiaBan.EditValue;
        }

        private void cmbNhomVatTuHangHoa_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (cmbNhomVatTuHangHoa.EditValue is Guid && (Guid)cmbNhomVatTuHangHoa.EditValue != Guid.Empty &&
                    (!Lists.VatTuHangHoa.DaDuocGiaoDich(_DoiTuong.ID) || Config.DuocPhepSuaMaHangKhiDaGiaoDich == DuocPhepSuaMaHangKhiDaGiaoDichHayKhong.DuocPhep))
                {
                    NhomVatTuHangHoa nhom = DanhMucNhomVatTuHangHoa.Instance.GetByID((Guid)cmbNhomVatTuHangHoa.EditValue);
                    if (nhom != null)
                    {
                        if (nhom.IsSach)
                        {
                            lblTitle.Text = "Sách";
                            lblGiaBia.Text = "Giá bán lẻ";
                            foreach (Control ctrl in this.Controls)
                            {
                                if (ctrl.Tag is string && ctrl.Tag.Equals("isSach")) ctrl.Enabled = true;
                            }
                        }
                        else
                        {
                            lblTitle.Text = "Hàng hóa khác sách";
                            lblGiaBia.Text = "Giá tại cửa hàng";
                            cmbNhaXuatBan.Text = "";
                            cmbTacGia.Text = "";
                            foreach (Control ctrl in this.Controls)
                            {
                                if (ctrl.Tag is string && ctrl.Tag.Equals("isSach")) ctrl.Enabled = false;
                            }
                        }
                    }

                        if (txtMa.Text == "")
                            txtMa.Text = DanhMucVatTuHangHoa.GenBarCode();
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void txtGiaMua_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                decimal chietKhauTinhNguocTheoGiaNET = 0;
                decimal giaNET = 0;
                decimal giaBanLe = 0;
                if (_EditState == EditState.CreatingNew || _EditState == EditState.Modifying)
                {
                    if (txtGiaBia.EditValue != null && txtGiaMua.EditValue != null &&
                        (decimal)txtGiaBia.EditValue > 0 && (decimal)txtGiaMua.EditValue > 0)
                    {
                        giaNET = (decimal)txtGiaMua.EditValue;
                        giaBanLe = (decimal)txtGiaBia.EditValue;

                        if (giaBanLe != 0)
                        {
                            chietKhauTinhNguocTheoGiaNET = ((giaBanLe - giaNET) * 100) / giaBanLe;
                            txtChietKhauMua.EditValue = Math.Round(chietKhauTinhNguocTheoGiaNET, 1);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void txtGiaBia_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                decimal chietKhauTinhNguocTheoGiaNET = 0;
                decimal giaNET = 0;
                decimal giaBanLe = 0;
                if (_EditState == EditState.CreatingNew || _EditState == EditState.Modifying)
                {
                    if (txtGiaBia.EditValue != null && txtGiaMua.EditValue != null &&
                        (decimal)txtGiaBia.EditValue > 0 && (decimal)txtGiaMua.EditValue > 0)
                    {
                        giaNET = (decimal)txtGiaMua.EditValue;
                        giaBanLe = (decimal)txtGiaBia.EditValue;

                        if (giaBanLe != 0)
                        {
                            chietKhauTinhNguocTheoGiaNET = ((giaBanLe - giaNET) * 100) / giaBanLe;
                            txtChietKhauMua.EditValue = Math.Round(chietKhauTinhNguocTheoGiaNET, 1);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void FrmEditHangHoa_Load(object sender, EventArgs e)
        {
            try
            {
                if (Config.DuocPhepSuaDanhMuc == DuocPhepSuaDanhMucHayKhong.KhongDuocPhep)
                {
                    cmbNhomVatTuHangHoa.popupList.Properties.ReadOnly = true;
                    txtMa.Properties.ReadOnly = true;
                    txtTen.Properties.ReadOnly = true;
                    txtTenNganGon.Properties.ReadOnly = true;
                    txtGiaMua.Properties.ReadOnly = true;
                    txtGiaBan.Properties.ReadOnly = true;
                    cmbNhaXuatBan.Enabled = false;
                    cmbNCC.Enabled = false;
                    cmbTacGia.Enabled = false;
                    txtChietKhauBanLe.Properties.ReadOnly = true;
                    txtChietKhauBan.Properties.ReadOnly = true;
                    txtChietKhauMua.Properties.ReadOnly = true;
                    cmbDonViTinh.Enabled = false;
                    txtKichThuoc.Properties.ReadOnly = true;
                    txtSoTrang.Properties.ReadOnly = true;
                    txtGhiChu.Properties.ReadOnly = true;
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void txtMa_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (txtMa.Text != null)
                {
                    Lists.VatTuHangHoa vthh = Lists.DanhMucVatTuHangHoa.GetByMa(txtMa.Text);
                    if (vthh == null) //Chưa có hàng này trong danh mục
                    {
                        lblCheckMaHang.Visible = false;
                    }
                    else
                    {
                        lblCheckMaHang.Visible = true;
                        lblCheckMaHang.Text = "Đã tồn tại mã này trong CSDL";
                    }
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void btnTheKho_Click(object sender, EventArgs e)
        {
            QuanLyKho.FrmThamSoTheKho frm = new QuanLyKho.FrmThamSoTheKho((_DoiTuong as Lists.VatTuHangHoa));
            frm.Show();
        }
    }
}
